import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import javax.print.Doc;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;

public class ShangJi3 {
    public static void main(String[] args) throws IOException, DocumentException {
//        //上机1 DOM4J解析
//        shangJi1();
//
//
//
//
//        try {
//            //上机2 DOM4J解析
//            UpdateShangJi2();
//        } catch (DocumentException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();

            deleteShangJi2();


    }



    public static void shangJi1() {

        Document doc = null;
        try{
            SAXReader saxReader = new SAXReader();
            doc = saxReader.read(new File("src/shiLi2.xml"));

        } catch (DocumentException e) {
            e.printStackTrace();
        }

        Element root = doc.getRootElement();

        for (Iterator itBrand = root.elementIterator(); itBrand.hasNext(); ) {
            Element brand = (Element) itBrand.next();
            System.out.println("品牌：" + brand.attributeValue("name"));
            for (Iterator itType = brand.elementIterator(); itType.hasNext(); ) {
                Element type = (Element) itType.next();
                System.out.println("\t型号："+type.attributeValue("name"));
            }
        }
    }


    public static void UpdateShangJi2() throws DocumentException, IOException {
        Document doc = null;

        try{
            SAXReader saxReader =  new SAXReader();
            doc = saxReader.read(new File("src/shili2.xml"));
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        Element root = doc.getRootElement();
        for (Iterator itBrand = root.elementIterator(); itBrand.hasNext();) {
            Element brands = (Element) itBrand.next();
            if (brands.attributeValue("name").equals("三星")) {
                //Attribute 属性
                Attribute name = brands.attribute("name");
                name.setText("华为");
            }
        }
        //文档中含有中文，设置格式在写入
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("utf-8");
        XMLWriter writer = new XMLWriter(new FileWriter("src/ShiLi2.xml"));
        writer.write(doc);
        writer.close();

    }

    public static void addShangJi2() throws IOException {
        Document doc =null;
        try {
            SAXReader saxReader = new SAXReader();
            doc = saxReader.read(new File("src/shiLi2.xml"));
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        //获取根节点
        Element root = doc.getRootElement();

        //创建一个名为Brand元素节点
        Element brandElement = DocumentHelper.createElement("Brand");
        //为Brand添加name属性 值：三星
        brandElement.addAttribute("name","三星");

        //创建一个名为Type元素节点
        Element TypeElement = DocumentHelper.createElement("Type");
        TypeElement.addAttribute("name","Not10");

        //为跟节点添加Brand元素
        root.add(brandElement);
        //为Brand元素添加子节点
        brandElement.add(TypeElement);

        //文档中含有中文，设置格式在写入
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("utf-8");
        XMLWriter writer = new XMLWriter(new FileWriter("src/ShiLi2.xml"));
        writer.write(doc);
        writer.close();
    }

    public static void deleteShangJi2() throws IOException, DocumentException {
        SAXReader saxReader = new SAXReader();
        Document doc = saxReader.read(new File("src/ShiLi2.xml"));
        //获取根节点
        Element root = doc.getRootElement();
        //循环根节点下的子节点
        for (Iterator itBrand = root.elementIterator(); itBrand.hasNext();) {
            Element isBrand = (Element) itBrand.next();
            //找到name为三星的节点
            if (isBrand.attributeValue("name").equals("三星")) {
                //删除节点
                root.remove(isBrand);
            }
        }
        //文档中含有中文，设置格式在写入
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("utf-8");
        XMLWriter writer = new XMLWriter(new FileWriter("src/ShiLi2.xml"));
        writer.write(doc);
        writer.close();
    }

}
